package com.she.iiicoursecenter;

import ken.app.helper.Helper;

import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_MainActivity;
import com.she.iiicoursecenter.activity.tab2_time.Tab2_MainActivity;
import com.she.iiicoursecenter.activity.tab3_reservation.Tab3_MainActivity;
import com.she.iiicoursecenter.activity.tab4_message.Tab4_MainActivity;
import com.she.iiicoursecenter.activity.template.BaseActivityGroup;
import com.she.iiicoursecenter.activity.template.TemplateActivity;
import com.she.iiicoursecenter.bean.CourseRceserveBundleBean;
import com.she.iiicoursecenter.bean.CourseSelectedListBundleBean;
import com.she.iiicoursecenter.bean.CourseSetBundleBean;
import com.she.iiicoursecenter.bean.CourseSingleBundleBean;
import com.she.iiicoursecenter.bean.MessageDetailBundleBean;
import com.she.iiicoursecenter.iface.IViewChangeBundleBean;
import com.she.iiicoursecenter.manager.ResourceManager;
import com.she.iiicoursecenter.view.BadgeView;

import android.app.ActivityGroup;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ViewFlipper;


@SuppressWarnings("deprecation")
public class MainActivityGroup extends BaseActivityGroup implements OnClickListener {
	//
	private enum TAB_SEL {TAB1, TAB2, TAB3, TAB4}
	private TAB_SEL mTabSel = TAB_SEL.TAB1;
	//
	private RelativeLayout	mSubViewContainer;
	private ViewFlipper		mTab1_Information;
	private ViewFlipper		mTab2_Time;
	private ViewFlipper		mTab3_Reservation;
	private ViewFlipper		mTab4_Message;
	private ImageView       mBadgeHolder;
	//
	private BadgeView		mBadgeView;
	//
	private String			mCurrentActivityClassName = "";
	//
	private boolean 		mFirst = true;;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//
		this.getApp().setMainActivityGroup(this);	// set reference to MainActivityGroup...
		//
		this.setContentView(R.layout.main);
		//
		setTab();
		//
		setTabState(TAB_SEL.TAB1);
		changeView(null, Tab1_MainActivity.class, null);
		
	}
	
	private void setTab(){
		//
		mSubViewContainer	= (RelativeLayout)this.findViewById(R.id.rl_subview_container);
		mTab1_Information	= (ViewFlipper)this.findViewById(R.id.img_maintab1);
		mTab2_Time			= (ViewFlipper)this.findViewById(R.id.img_maintab2);
		mTab3_Reservation	= (ViewFlipper)this.findViewById(R.id.img_maintab3);
		mTab4_Message		= (ViewFlipper)this.findViewById(R.id.img_maintab4);
		mBadgeHolder        = (ImageView)this.findViewById(R.id.img_badgeholder);
		//
		mBadgeView	= new BadgeView(this, mBadgeHolder);
		mBadgeView.setText(String.valueOf(getApp().msgCount));
		mBadgeView.setTextColor(Color.WHITE);
		mBadgeView.setBadgeBackgroundColor(Color.RED);
		mBadgeView.setTextSize(12);
		mBadgeView.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
		mBadgeView.show();
		//
		mTab1_Information.setOnClickListener(this);
		mTab2_Time.setOnClickListener(this);
		mTab3_Reservation.setOnClickListener(this);
		mTab4_Message.setOnClickListener(this);
		if (getApp().msgCount < 1) {
			setMessageUnreadCount(0, false);
		}
	}
	
	private void setTabState(TAB_SEL sel){
		mTabSel = sel;
		if (sel==TAB_SEL.TAB1)
			mTab1_Information.setDisplayedChild(1);
		else
			mTab1_Information.setDisplayedChild(0);
		if (sel==TAB_SEL.TAB2)
			mTab2_Time.setDisplayedChild(1);
		else
			mTab2_Time.setDisplayedChild(0);
		if (sel==TAB_SEL.TAB3)
			mTab3_Reservation.setDisplayedChild(1);
		else
			mTab3_Reservation.setDisplayedChild(0);
		if (sel==TAB_SEL.TAB4)
			mTab4_Message.setDisplayedChild(1);
		else
			mTab4_Message.setDisplayedChild(0);		
	}
	
	// set the badge number(unread message count)...& set visible or hidden...
	public void setMessageUnreadCount(int count, boolean show){
		mBadgeView.setText(String.valueOf(count));
		if (show){
			if (!mBadgeView.isShown())	mBadgeView.toggle();
		}else{
			if (mBadgeView.isShown())	mBadgeView.toggle();
		}
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		//
		ResourceManager.getInstance().clearAllCache();
		//
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	@Override
	public void onBackPressed() {
		String[] aa ={"是","否"};
		Helper.ShowMessage(this, "", "是否離開MIC青年講座？", 2, aa, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				ResourceManager.getInstance().clearAllCache();
				//
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		}, null, null);
//		super.onBackPressed();
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch(v.getId()){
		case R.id.img_maintab1:
			setTabState(TAB_SEL.TAB1);
			changeView(null, Tab1_MainActivity.class, null);
			break;
		case R.id.img_maintab2:
			setTabState(TAB_SEL.TAB2);
			changeView(null, Tab2_MainActivity.class, null);
			break;
		case R.id.img_maintab3:
			setTabState(TAB_SEL.TAB3);
			changeView(null, Tab3_MainActivity.class, null);
			break;
		case R.id.img_maintab4:
			setTabState(TAB_SEL.TAB4);
			changeView(null, Tab4_MainActivity.class, null);
			break;			
		}
	}
	
	// source and target activity's class...obj is anything you want to set into Bundle...
	public void changeView(Class current, Class next, Object obj){
		
		if (mFirst == false && mCurrentActivityClassName.equals(next.getSimpleName()))
			return;
		
		if (mFirst) mFirst = false;
		
		mCurrentActivityClassName = next.getSimpleName();
		
		if (current == null){			
			//
			//
		}else{
			//
			//
		}
		// fade out animation...
//    	Animation fadeout = AnimationUtils.loadAnimation(this, R.anim.zoomout);
//    	nextView.startAnimation(fadeout);
		
		mSubViewContainer.removeAllViews();
		
		//
		Bundle extras = new Bundle();
		
		if (obj != null){
			IViewChangeBundleBean bundlebean = (IViewChangeBundleBean)obj;
			// extract data from bundlebean...
			if(current.getSimpleName().equals("Tab1_MainActivity")){
				extras.putString("name", ((CourseSetBundleBean)bundlebean).name); // test only!!!!
				extras.putInt("itemId", ((CourseSetBundleBean)bundlebean).item_id); // test only!!!!
			}
			else if(current.getSimpleName().equals("Tab1_CourseSet")){
				extras.putInt("contentId", ((CourseSingleBundleBean)bundlebean).content_id); // test only!!!!
				extras.putString("contentName", ((CourseSingleBundleBean)bundlebean).content_name); // test only!!!!
			}
			else if(current.getSimpleName().equals("Tab1_CourseSingle")){
				if (bundlebean instanceof CourseRceserveBundleBean) {
					extras.putInt("contentId", ((CourseRceserveBundleBean)bundlebean).content_id); // test only!!!!
					extras.putString("code", ((CourseRceserveBundleBean)bundlebean).code); // test only!!!!
					extras.putString("name", ((CourseRceserveBundleBean)bundlebean).name); // test only!!!!
				}else if (bundlebean instanceof CourseSelectedListBundleBean) {
					extras.putString("date", ((CourseSelectedListBundleBean)bundlebean).date); // test only!!!!
					extras.putString("time", ((CourseSelectedListBundleBean)bundlebean).time); // test only!!!!
					extras.putString("slot", ((CourseSelectedListBundleBean)bundlebean).slot); // test only!!!!
				}

			}
			else if(current.getSimpleName().equals("Tab2_MainActivity")){
				extras.putString("date", ((CourseSelectedListBundleBean)bundlebean).date); // test only!!!!
				extras.putString("time", ((CourseSelectedListBundleBean)bundlebean).time); // test only!!!!
				extras.putString("slot", ((CourseSelectedListBundleBean)bundlebean).slot); // test only!!!!
			}
			else if(current.getSimpleName().equals("Tab3_MainActivity")){
				extras.putInt("contentId", ((CourseSingleBundleBean)bundlebean).content_id); // test only!!!!
				extras.putString("contentName", ((CourseSingleBundleBean)bundlebean).content_name); // test only!!!!
			}
			else if(current.getSimpleName().equals("Tab2_CourseSelectedList")){
				extras.putInt("contentId", ((CourseSingleBundleBean)bundlebean).content_id); // test only!!!!
				extras.putString("contentName", ((CourseSingleBundleBean)bundlebean).content_name); // test only!!!!
			}
			else if(current.getSimpleName().equals("Tab4_MainActivity")){
				extras.putInt("postition", ((MessageDetailBundleBean)bundlebean).postition); // test only!!!!
			}
			else if(current.getSimpleName().equals("Tab1_CourseReserve")){
//				extras.putInt("contentId", ((CourseSingleBundleBean)bundlebean).content_id); // test only!!!!
//				extras.putString("contentName", ((CourseSingleBundleBean)bundlebean).content_name); // test only!!!!
				if (bundlebean instanceof CourseSingleBundleBean) {
					extras.putInt("contentId", ((CourseSingleBundleBean)bundlebean).content_id); // test only!!!!
					extras.putString("contentName", ((CourseSingleBundleBean)bundlebean).content_name); // test only!!!!
				}else if (bundlebean instanceof CourseSelectedListBundleBean) {
					extras.putString("date", ((CourseSelectedListBundleBean)bundlebean).date); // test only!!!!
					extras.putString("time", ((CourseSelectedListBundleBean)bundlebean).time); // test only!!!!
					extras.putString("slot", ((CourseSelectedListBundleBean)bundlebean).slot); // test only!!!!
				}
			}
		}


		// set anything you want to carry with...
		View nextView = getLocalActivityManager().startActivity(next.getSimpleName() ,
				new Intent(MainActivityGroup.this, next).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtras(extras)).getDecorView();
		
		// fade in animation...
		if (getApp().fadein) {
	    	Animation fadein = AnimationUtils.loadAnimation(this, R.anim.zoomin);
	    	nextView.startAnimation(fadein);
	    	getApp().fadein = false;
		}
		if (getApp().fadeout) {
	    	Animation fadeout = AnimationUtils.loadAnimation(this, R.anim.zoomout);
	    	nextView.startAnimation(fadeout);
	    	getApp().fadeout = false;
		}
		
    	mSubViewContainer.addView(nextView);
		
	}

}
